package com.yuwei.order

/**
 * 添加地址页面
 */
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.tencent.mmkv.MMKV
import com.yuwei.base.RoutePath
import com.yuwei.common.mvi.BaseMviActivity
import com.yuwei.common.mvi.UiState
import com.yuwei.database.entity.AddressEntity
import com.yuwei.order.databinding.ActivityAddAddressBinding
@Route(path = RoutePath.MODULE_ORDER_ADD_ADDRESS_ACTIVITY)
class AddAddressActivity : BaseMviActivity<ActivityAddAddressBinding,OrderViewModel>() {
    @Autowired
    @JvmField
    var addressEntity: AddressEntity? = null
    override fun successData(it: UiState.onSuccess<*>) {
        when(it.type){
            "insertAddress"->{
                ToastUtils.showShort("添加成功")
                finish()
            }
            "updateAddress"->{
                ToastUtils.showShort("修改成功")
                finish()
            }
        }
    }

    override fun failureData(error: String) {
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        if(addressEntity != null){
            binding.etReceivingPerson.setText(addressEntity?.name)
            binding.etChoseAddress.text = addressEntity?.address
            binding.etFullAddress.setText(addressEntity?.fullAddress)
            binding.etPhoneNumber.setText(addressEntity?.phone)
            binding.etFullAddress.setText(addressEntity?.address)
            binding.etTag.setText(addressEntity?.tag)
        }
    }

    override fun initData() {
        binding.ivBack.setOnClickListener {
            finish()
        }
        binding.etChoseAddress.setOnClickListener{
            val addressPicker= AddressPicker(this)
            addressPicker.setTitle("请选择地址")
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                binding.etChoseAddress.text="${province.name}${city.name}${county.name}"
            }
            addressPicker.show()
        }
        binding.btnSaveAddress.setOnClickListener {
            if(binding.etReceivingPerson.text.isEmpty()){
                binding.etReceivingPerson.error="请填写收货人"
                return@setOnClickListener
            }
            if(binding.etChoseAddress.text == "选择地区"){
                binding.etChoseAddress.error="请选择地址"
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileExact(binding.etPhoneNumber.text)){
                binding.etPhoneNumber.error="请填写正确的手机号"
                return@setOnClickListener
            }
            if(binding.etPhoneNumber.text.isEmpty()){
                binding.etPhoneNumber.error="请填写手机号"
                return@setOnClickListener
            }
            if(binding.etFullAddress.text.isEmpty()){
                binding.etFullAddress.error="请填写详细地址"
                return@setOnClickListener
            }
            if(binding.etTag.text.isEmpty()){
                binding.etTag.error="请填写标签"
                return@setOnClickListener
            }
           if(addressEntity == null){
               vm.sendIntent(OrderIntent.insertAddress(AddressEntity(
                   0,
                   binding.etChoseAddress.text.toString(),
                   binding.etFullAddress.text.toString(),
                   binding.etReceivingPerson.text.toString(),
                   binding.etPhoneNumber.text.toString(),
                   MMKV.defaultMMKV().getInt("userId",0),
                   binding.etTag.text.toString(),
                   false)))
           }else{
               vm.sendIntent(OrderIntent.updateAddress(AddressEntity(
                   addressEntity!!.id,
                   binding.etChoseAddress.text.toString(),
                   binding.etFullAddress.text.toString(),
                   binding.etReceivingPerson.text.toString(),
                   binding.etPhoneNumber.text.toString(),
                   MMKV.defaultMMKV().getInt("userId",0),
                   binding.etTag.text.toString(),
                   false)))
           }
        }
    }
}